﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


/// <summary>
/// 震动任意对象的坐标
/// </summary>
[AddComponentMenu("Behavior/Shake")]
public class BhvShake : MonoBehaviour
{
    [Tooltip("移动模式，移动模式不会还原对象坐标回到shake 发生前的状态")]
    public bool moveingMode = false;

    [Tooltip("衰退模式，会随着震动时间强度逐渐衰退")]
    public bool decayMode = false;

    [Tooltip("阻尼，震动时半径逐渐缩小的速率")]
    public float damping = 0.01f;

    public float shakeTime = 0.5f;

    [Tooltip("向各个方向的震动频率")]
    public Vector3 intensity = new Vector3(0.05f, 0.05f, 0.05f);

    //震动用到的值
    private Vector3 _intensity = new Vector3(0.05f, 0.05f, 0.05f);

    private Vector3 _originPos;

    private bool isShaking = false;

    private float _timer = 0.5f;

    private void Awake()
    {
        this._originPos = transform.localPosition;
        this._intensity.x = this.intensity.x;
        this._intensity.y = this.intensity.y;
        this._intensity.z = this.intensity.z;
    }

    public void OnEventShake()
    {
        Shake();
    }

    public void Shake(float time = 0.5f, float intensityScale = 1.0f)
    {
        if (!this.isShaking)
        {
            this._originPos = transform.localPosition;
        }

        if (time > 0)
        {
            this._timer = time;
        }

        this._intensity.x = this.intensity.x * intensityScale;
        this._intensity.y = this.intensity.y * intensityScale;
        this._intensity.z = this.intensity.z * intensityScale;
        this._timer = this.shakeTime;
        this.isShaking = true;

    }

    // Update is called once per frame
    void Update()
    {
        if (!this.isShaking) return;
        var dt = Time.deltaTime;
        this._timer -= dt;

        if (this._timer <= 0)
        {
            this.isShaking = false;
            if (!this.moveingMode) transform.localPosition = this._originPos;
        }

        float sx;
        float sy;
        float sz;
        sx = Random.Range(-1.0f, 1.0f) * this._intensity.x;
        sy = Random.Range(-1.0f, 1.0f) * this._intensity.y;
        sz = Random.Range(-1.0f, 1.0f) * this._intensity.z;

        if (this.decayMode)
        {
            if (this._intensity.x > 0.01)
            {
                this._intensity.x -= this._intensity.x * this.damping * dt * 60;
            }
            else
            {
                this._intensity.x = 0;
            }

            if (this._intensity.y > 0.01)
            {
                this._intensity.y -= this._intensity.y * this.damping * dt * 60;
            }
            else
            {
                this._intensity.y = 0;
            }

            if (this._intensity.z > 0.01)
            {
                this._intensity.z -= this._intensity.z * this.damping * dt * 60;
            }
            else
            {
                this._intensity.z = 0;
            }

            if (this._intensity.y < 0.01 && this._intensity.x < 0.01 && this._intensity.z < 0.01)
            {
                this.isShaking = false;
                transform.localPosition = this._originPos;
            }
        }

        if (this.moveingMode)
        {
            transform.localPosition += new Vector3(sx, sy, sz);
        }
        else
        {
            transform.localPosition = this._originPos + new Vector3(sx, sy, sz);
        }


    }
}
